package com.qf.erp_system_admin.service.impl;

import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.symmetric.SM4;
import com.qf.erp_system_admin.entity.User;
import com.qf.erp_system_admin.entity.UserVo;
import com.qf.erp_system_admin.mapper.UserMapper;
import com.qf.erp_system_admin.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author baishi Li
 * @date
 * @desc
 */
@Service
public class UserServiceImpl implements UserService {
    // 固定的密钥字符串
    String keyStr = "ksiaoqlaksiwjske";


    @Autowired
    private UserMapper mapper;
    @Override
    public boolean login(User user , HttpSession session) {
        // 将密钥字符串转换为字节数组
        byte[] key = keyStr.getBytes(CharsetUtil.CHARSET_UTF_8);
        // 创建一个加密实例
        SM4 sm4 = (SM4) SmUtil.sm4(key);
        //加密
        String encryptHex = sm4.encryptHex(user.getLgPassword());
        user.setLgPassword(encryptHex);
       User login =  mapper.login(user);
       if (login != null){
           session.setAttribute("UserInfo",login);
           return true;
       }else {
           return false;
       }
    }

    @Override
    public User getUserInfo(HttpSession session) {
        User info = (User) session.getAttribute("UserInfo");
        return info;
    }

    @Override
    public void logout(HttpSession session) {
        session.invalidate();
    }

    @Override
    public List<User> findAll(HashMap<String, String> map) {
            return mapper.findAll(map);
    }

    @Override
    public int edit(User user) {
        // 将密钥字符串转换为字节数组
        byte[] key = keyStr.getBytes(CharsetUtil.CHARSET_UTF_8);
        // 创建一个密钥实例
        SM4 sm4 = (SM4) SmUtil.sm4(key);
        //加密
        String encryptHex = sm4.encryptHex(user.getLgPassword());
        user.setLgPassword(encryptHex);
        return mapper.edit(user);
    }

    @Override
    public UserVo powerSelect(User user) {
        if (user.getLgPassword() != null){
            // 将密钥字符串转换为字节数组
            byte[] key = keyStr.getBytes(CharsetUtil.CHARSET_UTF_8);
            // 创建一个密钥实例
            SM4 sm4 = (SM4) SmUtil.sm4(key);
            //加密
            String encryptHex = sm4.encryptHex(user.getLgPassword());
            user.setLgPassword(encryptHex);
        }
        return mapper.powerSelect(user);
    }

    @Override
    public String decryptStr(String password) {
        // 将密钥字符串转换为字节数组
        byte[] key = keyStr.getBytes(CharsetUtil.CHARSET_UTF_8);
        // 创建一个密钥实例
        SM4 sm4 = (SM4) SmUtil.sm4(key);
        //解密
        String decryptStr = sm4.decryptStr(password, CharsetUtil.CHARSET_UTF_8);
        return decryptStr;
    }

    //从excel导入数据
    @Override
    public void saveBatch(ArrayList<User> list) {
        mapper.saveBatch(list);
    }


    @Override
    public int del(int lgId) {
        return mapper.del(lgId);
    }

    @Override
    public int add(User user) {
        // 将密钥字符串转换为字节数组
        byte[] key = keyStr.getBytes(CharsetUtil.CHARSET_UTF_8);
        // 创建一个加密实例
        SM4 sm4 = (SM4) SmUtil.sm4(key);
        //加密
        String encryptHex = sm4.encryptHex(user.getLgPassword());
        user.setLgPassword(encryptHex);
        return mapper.add(user);
    }
}
